<?php
class Admin_CategoryController extends Zend_Controller_Action {
	public function preDispatch() {
		$auth = Zend_Auth::getInstance ();
		if (! $auth->hasIdentity ()) {
			if ($this->_request->getActionName () != "login") {
				$this->_redirect ( "/admin/index/login" );
			}
		}
	}
	public function init() {
		/* Initialize action controller here */
		$this->_model = new Default_Model_DbTable_Category ();
		$auth = Zend_Auth::getInstance ();
		$infoUser = $auth->getIdentity ();
		$this->view->fullName = $infoUser->USERNAME;
		Zend_Layout::startMvc ( array (
				"layoutPath" => APPLICATION_PATH . "/layouts/scripts",
				"layout" => "admin" 
		) );
	}
	public function indexAction() {
		$status = $this->getRequest ()->getParam ( "sorting" );
		$this->view->headTitle ( "Quản lý danh mục", "PREPEND" );
		$this->view->title = "Quản lý danh mục";
		if ($status == null)
			$cat = $this->_model->getAllCategory ();
		else
			$cat = $this->_model->getCategoryByStatus ( $status );
		$paginator = Zend_Paginator::factory ( $cat );
		$paginator->setItemCountPerPage ( 9 );
		$paginator->setPageRange ( 5 );
		$currentPage = $this->_request->getParam ( "page", 1 );
		$paginator->setCurrentPageNumber ( $currentPage );
		$this->view->allCat = $paginator;
	}
	public function addAction() {
		$this->view->headTitle ( "Thêm danh mục", "PREPEND" );
		$this->view->title = "Quản lý danh mục";
		$this->view->allCat = $this->_model->getAllCategory ();
		$upload = new Private_Upload ();
		if ($this->getRequest ()->isPost ()) {
			$Sub = $this->_request->getParam ( "psize" );
			$CatName = $this->_request->getParam ( "CatName" );
			$Desc = $this->_request->getParam ( "Desc" );
			$Stt = $this->_request->getParam ( "Stt" );
			if ($_FILES ['Img'] ["name"]) {
				$img = "/ishali/public/uploaded/images/category/" . $_FILES ['Img'] ["name"];
				$upload->upload ( "category" );
			} else {
				$img = "/ishali/public/uploaded/images/category/no_image.jpg";
			}
			$this->_model->addCategory ( $Sub, $CatName, $Desc, $img, $Stt );
			$this->_redirect ( "/admin/category" );
		}
	}
	public function editAction() {
		$this->view->headTitle ( "Chỉnh sửa danh sách", "PREPEND" );
		$this->view->title = "Quản lý danh sách";
		$this->view->allCat = $this->_model->getAllCategory ();
		$upload = new Private_Upload ();
		$id = $this->_getParam ( "id" );
		$this->view->cat = $this->_model->getCategoryById ( $id );
		if ($this->getRequest ()->isPost ()) {
			$Sub = $this->_request->getParam ( "psize" );
			$CatName = $this->_request->getParam ( "CatName" );
			$Desc = $this->_request->getParam ( "Desc" );
			$Stt = $this->_request->getParam ( "Stt" );
			if ($Stt == 0) {
				$news = new Default_Model_DbTable_News ();
				$list = $news->getNewsByCategoryId ( $id, null );
				foreach ( $list as $l ) {
					$news->updateStatus ( $l ["id"], 0 );
				}
				if ($_FILES ['Img'] ["name"]) {
					$img = "/ishali/public/uploaded/images/category/" . $_FILES ['Img'] ["name"];
					$upload->upload ( "category" );
				} else {
					$img = "/ishali/public/uploaded/images/category/no_image.jpg";
				}
				$upload->upload ( "category" );
				$this->_model->updateCategory ( $id, $Sub, $CatName, $Desc, $img, $Stt );
				$this->_redirect ( "/admin/category" );
			} else {
				$news = new Default_Model_DbTable_News ();
				$list = $news->getNewsByCategoryId ( $id, null );
				foreach ( $list as $l ) {
					$news->updateStatus ( $l ["id"], 1 );
				}
				if ($_FILES ['Img'] ["name"]) {
					$img = "/ishali/public/uploaded/images/category/" . $_FILES ['Img'] ["name"];
					$upload->upload ( "category" );
				} else {
					$img = "/ishali/public/uploaded/images/category/no_image.jpg";
				}
				$upload->upload ( "category" );
				$this->_model->updateCategory ( $id, $Sub, $CatName, $Desc, $img, $Stt );
				$this->_redirect ( "/admin/category" );
			}
		}
	}
	public function delAction() {
		$id = $this->_getParam ( "id" );
		$news = new Default_Model_DbTable_News ();
		$list = $news->getNewsByCategoryId ( $id, null );
		foreach ( $list as $l ) {
			$news->removeNews ( $l ["id"] );
		}
		$this->_model->removeCategory ( $id );
		$this->_redirect ( "/admin/category" );
	}
	public function activeAction() {
		$id = $this->_getParam ( "id" );
		$this->_model->updatestt ( $id );
		$cate = $this->_model->getCategoryById ( $id );
		$news = new Default_Model_DbTable_News ();
		$list = $news->getNewsByCategoryId ( $id, null );
		foreach ( $list as $l ) {
			$news->updateStatus ( $l ["id"], $cate ["STATUS"] );
		}
		$this->_redirect ( "/admin/category" );
	}
}